/**
 * 2012-7-8
 * jqsl2012@163.com
 */
package shop.controller.manage.news;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.BaseController;
import shop.core.common.bean.JSONResult;
import shop.core.common.bean.ManageContainer;
import shop.core.common.dao.page.PagerModel;
import shop.services.manage.news.NewsService;
import shop.services.manage.news.bean.News;
import shop.services.manage.organize.OrganizeService;
import shop.services.manage.organize.bean.Organize;
import shop.services.manage.system.bean.Role;
import shop.services.manage.system.bean.User;
import shop.services.manage.system.impl.RoleService;

import javax.servlet.http.HttpServletRequest;


/**
 * 文章管理
 *
 * @author huangf
 * @author jeeshopfans
 */
@Controller
@RequestMapping("/manage/news/")
public class NewsAction extends BaseController<News> {
    private static final String page_toList = "/manage/news/newsList";
    private static final String page_toEdit = "/manage/news/newsEdit";
    private static final String page_toAdd = "/manage/news/newsAdd";

    private NewsAction() {
        super.page_toList = page_toList;
        super.page_toAdd = page_toAdd;
        super.page_toEdit = page_toEdit;
    }

    @Autowired
    private NewsService newsService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private OrganizeService organizeService;

    @Autowired
    public NewsService getService() {
        return newsService;
    }

    @RequestMapping("toPendingList")
    public String toPendingList() {
        return "/manage/news/newsPendingList";
    }

    /**
     * 跳转到爱优享介绍页
     *
     * @return
     */
    @RequestMapping("toHomeDetail")
    public String toHomeDetail() {
        return "/manage/news/homeDetail";
    }

    /**
     * 跳转到通知页面
     *
     * @return
     */
    @RequestMapping("toNotice")
    public String toNotice() {
        return "/manage/news/notice";
    }

    /**
     * 跳转到帮助中心页面
     *
     * @return
     */
    @RequestMapping("toHelp")
    public String toHelp() {
        return "/manage/news/help";
    }


    /**
     * 发布资讯
     *
     * @return jsonResult
     * @throws Exception
     */
    @Override
    @RequestMapping(value = "insertJson", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertJson(HttpServletRequest request, @ModelAttribute("news") News news) throws Exception {
        jsonResult = new JSONResult();
        User user = (User) request.getSession().getAttribute(ManageContainer.manage_session_user_info);
        String rid = user.getRid();
        Role userRole = roleService.selectById(rid);
        if (userRole.getRole_name().equals("机构账号")) {
            news.setType("o");
            news.setApprovalStatus("init");
            Organize organize = new Organize();
            organize.setPrincipalPhone(user.getUsername());
            organize = organizeService.selectByPrincipalPhone(organize);
            if (organize == null)
                return JSONResult.error("该用户机构已删除，请于管理员联系");
            news.setOrganizeID(organize.getId());
            if (!organize.getStatus().equals("1")) {
                return JSONResult.error("该机构还未认证通过，请先认证。");
            }
        } else {
            news.setOrganizeID("-1");
            news.setType("ad");
            news.setApprovalStatus("pass");
        }
        news.setCreateAccount(user.getId());
        getService().insert(news);
        return jsonResult;
    }

    /**
     * 分页获取数据列表
     *
     * @param request 请求
     * @param news    对象参数
     * @return 分页数据模型
     */
    @Override
    public PagerModel loadData(HttpServletRequest request, News news) {
        User user = (User) request.getSession().getAttribute(ManageContainer.manage_session_user_info);
        String rid = user.getRid();
        Role userRole = roleService.selectById(rid);
        if (userRole.getRole_name().equals("机构账号")) {
            Organize organize = new Organize();
            organize.setPrincipalPhone(user.getUsername());
            organize = organizeService.selectByPrincipalPhone(organize);
            news.setOrganizeID(organize.getId());
            return super.loadData(request, news);
        }
        return super.loadData(request, news);
    }

    @RequestMapping(value = "/pendingList", method = RequestMethod.GET)
    @ResponseBody
    public PagerModel pendingList(HttpServletRequest request, News news) {
        int offset = 0;
        int pageSize = 10;
        if (request.getParameter("start") != null) {
            offset = Integer.parseInt(request.getParameter("start"));
        }
        if (request.getParameter("length") != null) {
            pageSize = Integer.parseInt(request.getParameter("length"));
        }
        if (offset < 0)
            offset = 0;
        if (pageSize < 0) {
            pageSize = 10;
        }
        news.setOffset(offset);
        news.setPageSize(pageSize);
        news.setType("o");
        PagerModel pager = getService().pendingList(news);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    @RequestMapping("toApproval")
    public String toApproval(@ModelAttribute("e") News e, ModelMap model) {
        e = getService().selectOne(e);
        model.addAttribute("e", e);
        return "/manage/news/newsApprove";
    }

    @RequestMapping("toView")
    public String toView(@ModelAttribute("e") News e, ModelMap model) {
        e = getService().selectOne(e);
        model.addAttribute("e", e);
        return "/manage/news/newsView";
    }

    @Override
    public JSONResult updateJson(HttpServletRequest request, @ModelAttribute("e") News news) throws Exception {
        News oldNews=newsService.selectOne(news);
        if (oldNews.getApprovalStatus().equals("fail")||oldNews.getApprovalStatus().equals("closed")){
            news.setApprovalStatus("init");
        }
        return super.updateJson(request, news);
    }
}
